package com.roy.spring.ioc.util;


import java.lang.annotation.Annotation;
import java.lang.reflect.AnnotatedElement;

/**
 * 类加载工具
 */
public class ClassUtil {

	/**
	 * 获取当前线程的类加载器
	 * @return
	 */
	public static ClassLoader getClassLoader(){
		return Thread.currentThread().getContextClassLoader();
	}


	/**
	 * 初始化实例
	 * @param className
	 * @param paramType
	 * @param o
	 * @return
	 */
	public static Object newInstance(String className, Class<?>[] paramType,
			Object[] o) {
		try {
			return Class.forName(className).getConstructor(paramType)
					.newInstance(o);
		} catch (Exception e) {
			throw new RuntimeException("创建对象失败", e);
		}
	}


	/**
	 * 获取注解方法
	 * @param targetClass
	 * @param methodName
	 * @param parameterTypes
	 * @param annotationClass
	 * @param <T>
	 * @return
	 */
	public static <T extends Annotation> T getMethodAnnotation(
			Class<?> targetClass, String methodName, Class<?>[] parameterTypes,
			Class<T> annotationClass) {
		try {
			return targetClass.getMethod(methodName, parameterTypes)
					.getAnnotation(annotationClass);
		} catch (NoSuchMethodException e) {
			throw new RuntimeException("找不到这个方法" + methodName, e);
		}
	}

}
